package com.live.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.live.adapter.LiveSearchSimpleAdapter;
import com.live.common.Constant;
import com.live.task.LiveSearch;
import com.live.tools.LiveTools;

public class LiveSearchActivity extends LiveBaseActivity {

	private ListView matchList = null;
	private Button searchBtn = null;
	private EditText searchKey = null;
	private View liveSearchFoot = null;
	private LiveSearchSimpleAdapter adapter = null;
	private List<Map<String, Object>> liveMatchList = new ArrayList<Map<String, Object>>();
	private int page = 0;
	private String searchKeyStr = "";
	private ProgressBar progressBar;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.live_search);

		liveSearchFoot = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.live_search_foot, null, false);
		liveSearchFoot.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!"".equals(searchKeyStr)) {
					TextView loadMore = (TextView) liveSearchFoot.findViewById(R.id.load_more);
					loadMore.setVisibility(View.GONE);// 按钮不可见
					progressBar.setVisibility(View.VISIBLE);// 将进度条可见
					page = page + 1;
					String url = Constant.SITE_URL + "live_search.php?locale=" + locale + "&key=" + searchKeyStr
							+ "&keySimp=" + LiveTools.simpToTrad(searchKeyStr, locale) + "&page=" + page;
					LiveSearch searchTask = new LiveSearch(context, adapter, liveMatchList, liveSearchFoot, true);
					searchTask.execute(url);
				}
			}
		});

		progressBar = (ProgressBar) liveSearchFoot.findViewById(R.id.load_more_prog_bar);

		searchKey = (EditText) findViewById(R.id.searchKey);

		searchBtn = (Button) findViewById(R.id.searchBtn);
		searchBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				searchKeyStr = searchKey.getText().toString();
				if (!"".equals(searchKeyStr)) {
					page = 0;

					// 初始化list
					for (int i = liveMatchList.size() - 1; i >= 0; i--) {
						liveMatchList.remove(i);
					}

					// 初始化listFoot
					TextView loadMore = (TextView) liveSearchFoot.findViewById(R.id.load_more);
					loadMore.setVisibility(View.VISIBLE);
					progressBar.setVisibility(View.GONE);

					String url = Constant.SITE_URL + "live_search.php?locale=" + locale + "&key=" + searchKeyStr
							+ "&keySimp=" + LiveTools.simpToTrad(searchKeyStr, locale) + "&page=" + page;
					LiveSearch searchTask = new LiveSearch(context, adapter, liveMatchList, liveSearchFoot, false);
					searchTask.execute(url);
				}
			}
		});

		adapter = new LiveSearchSimpleAdapter(context, liveMatchList, R.layout.search_list, new String[] { "matchName",
				"matchTime", "homeTeam", "homeTeamScore", "visitTeam", "visitTeamScore", "matchStatus", "matchRecord",
				"matchWatch" }, new int[] { R.id.matchName, R.id.matchTime, R.id.homeTeam, R.id.homeTeamScore,
				R.id.visitTeam, R.id.visitTeamScore, R.id.matchStatus, R.id.matchRecord, R.id.matchWatch });

		matchList = (ListView) findViewById(R.id.matchList);
		matchList.addFooterView(liveSearchFoot);
		matchList.setAdapter(adapter);
		matchList.setOnItemClickListener(new OnItemClickListener() {

			// 从当前画面到比分明细画面跳转，并传值。
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				Map<String, Object> sportsMatch = liveMatchList.get(arg2);

				Intent intent = new Intent();

				intent.setClass(context, LiveDetailActivity.class);

				// 实例化 Bundle，设置需要传递的参数
				Bundle bundle = new Bundle();
				bundle.putString("matchId", sportsMatch.get("matchId").toString());
				bundle.putString("matchOptaId", sportsMatch.get("matchOptaId").toString());
				bundle.putString("matchName", sportsMatch.get("matchName").toString());
				if (sportsMatch.get("matchRecord") != null) {
					bundle.putString("matchRecord", sportsMatch.get("matchRecord").toString());
				}
				bundle.putString("matchCategory", sportsMatch.get("matchCategory").toString());
				bundle.putString("matchTime", sportsMatch.get("matchTime").toString());
				bundle.putString("homeTeam", sportsMatch.get("homeTeam").toString());
				bundle.putString("homeTeamName", sportsMatch.get("homeTeamName").toString());
				bundle.putString("homeTeamScore", sportsMatch.get("homeTeamScore").toString());
				bundle.putString("homeTeamPic", sportsMatch.get("homeTeamPic").toString());
				bundle.putString("visitTeam", sportsMatch.get("visitTeam").toString());
				bundle.putString("visitTeamName", sportsMatch.get("visitTeamName").toString());
				bundle.putString("visitTeamScore", sportsMatch.get("visitTeamScore").toString());
				bundle.putString("visitTeamPic", sportsMatch.get("visitTeamPic").toString());
				bundle.putString("matchStatus", sportsMatch.get("matchStatus").toString());
				bundle.putString("matchWatch", sportsMatch.get("matchWatch").toString());

				intent.putExtras(bundle);

				context.startActivity(intent);
			}
		});
	}
}